package com.example.floristshop;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.floristshop.adapters.RecordAdapter;
import com.example.floristshop.database.Flower;
import com.example.floristshop.database.Record;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SearchRecordActivity extends AppCompatActivity {
    Toolbar toolbar;
    EditText flower_et, money_min_et, money_max_et;
    ImageView search_btn;
    ListView listView;

    RecordAdapter adapter;

    List<Record> recordList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_record);

        // init data
        recordList = LitePal.findAll(Record.class);

        // get view
        toolbar = (Toolbar)findViewById(R.id.search_record_toolbar);
        flower_et = (EditText)findViewById(R.id.search_record_flower_text);
        money_min_et = (EditText)findViewById(R.id.search_record_money_min);
        money_max_et = (EditText)findViewById(R.id.search_record_money_max);
        search_btn = (ImageView)findViewById(R.id.search_record_search_button);
        listView = (ListView)findViewById(R.id.search_record_listview);

        // set view
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        adapter = new RecordAdapter(SearchRecordActivity.this, R.layout.record_item, recordList);
        listView.setAdapter(adapter);
        search_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String flower_text, money_min_string, money_max_string;
                flower_text = flower_et.getText().toString();
                money_min_string = money_min_et.getText().toString();
                money_max_string = money_max_et.getText().toString();
                List<Flower> flowerList = LitePal.where("name like ? or type like ? or origin like ?","%"+flower_text+"%", "%"+flower_text+"%", "%"+flower_text+"%").find(Flower.class);

                double min = money_min_string.equals("")?0.0:Double.parseDouble(money_min_string);
                double max = money_max_string.equals("")?Double.MAX_VALUE:Double.parseDouble(money_max_string);
                recordList = LitePal.where("money>=? and money<=?", min+"", max+"")
                        .order("timestamp")
                        .find(Record.class);
                Collections.reverse(recordList);

                List<Record> newList = new ArrayList<>();
                for(Record record: recordList){
                    if(is_in_list(record.getFlower_id(), flowerList)){
                        newList.add(record);
                    }
                }

                RecordAdapter new_adapter = new RecordAdapter(SearchRecordActivity.this, R.layout.record_item, newList);
                listView.setAdapter(new_adapter);
            }
        });
    }

    private boolean is_in_list(long flower_id, List<Flower> flowerList){
        for(Flower flower: flowerList){
            if(flower_id == flower.getFlower_id()){
                return true;
            }
        }
        return false;
    }
}